package org.xcorpio.java8.chapter12;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoField;

public class NewDateAndTime {

    public static void main(String[] args) {
        createAndReadDate();
        System.out.println();
        readDateUsingTemporalField();
        System.out.println();
        createAndReadTime();
        System.out.println();
        parse();
        System.out.println();
        combiningDateAndTime();
        System.out.println();
        instant();
        System.out.println();
        durationPeriod();
    }

    private static void durationPeriod() {
        Duration ofSeconds = Duration.ofSeconds(3);
        System.out.println(ofSeconds);
        LocalTime time1 = LocalTime.of(1, 1);
        LocalTime time2 = LocalTime.of(1, 2);
        Duration duration = Duration.between(time1, time2);
        System.out.println(duration);
        Period period = Period.of(1, 1, 1);
        System.out.println(period);
    }

    private static void instant() {
        Instant instant1 = Instant.ofEpochSecond(3);
        System.out.println(instant1);
        Instant instant2 = Instant.ofEpochSecond(3, 0);
        System.out.println(instant2);
        Instant now = Instant.now();
        System.out.println(now);
        System.out.println(now.get(ChronoField.NANO_OF_SECOND));
    }

    private static void combiningDateAndTime() {
        LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 12, 45, 20);
        System.out.println(dt1);
        LocalDate date = LocalDate.now();
        System.out.println(date);
        LocalTime time = LocalTime.now();
        System.out.println(time);
        LocalDateTime dt2 = LocalDateTime.of(date, time);
        System.out.println(dt2);
        LocalDateTime dt3 = date.atTime(time);
        System.out.println(dt3);
        LocalDateTime dt4 = time.atDate(date);
        System.out.println(dt4);
        System.out.println(dt4.toLocalDate());
        System.out.println(dt4.toLocalTime());
    }

    private static void parse() {
        LocalDate date = LocalDate.parse("2016-11-11");
        System.out.println(date);
        LocalTime time = LocalTime.parse("20:12:11");
        System.out.println(time);
    }

    private static void createAndReadTime() {
        LocalTime time = LocalTime.of(22, 22, 22);
        System.out.println(time);
        int hour = time.getHour();
        System.out.println(hour);
        int minute = time.getMinute();
        System.out.println(minute);
        int second = time.getSecond();
        System.out.println(second);
    }

    private static void readDateUsingTemporalField() {
        LocalDate now = LocalDate.now();
        int year = now.get(ChronoField.YEAR);
        System.out.println(year);
        int month = now.get(ChronoField.MONTH_OF_YEAR);
        System.out.println(month);
        int day = now.get(ChronoField.DAY_OF_MONTH);
        System.out.println(day);
    }

    private static void createAndReadDate() {
        LocalDate date = LocalDate.of(2015, 11, 11);
        System.out.println(date);
        int year = date.getYear();
        System.out.println(year);
        Month month = date.getMonth();
        System.out.println(month);
        int day = date.getDayOfMonth();
        System.out.println(day);
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        System.out.println(dayOfWeek);
        int len = date.lengthOfMonth();
        System.out.println(len);
        boolean leap = date.isLeapYear();
        System.out.println(leap);

        LocalDate now = LocalDate.now();
        System.out.println(now);
    }
}
